package controllers;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import lib.Subject;
import models.Event;

public class EventController
	extends ControllerAbstract 
{
public ArrayList<Event> event_list = new ArrayList<Event>(); 
	
	public EventController()
	{
		registerController();
		event_list = read_Event();
	}
		
		public ArrayList<Event> read_Event()
		{
			
			String filename = "event.ser"; 
			try
			{
				FileInputStream fis = new FileInputStream(filename);
				ObjectInputStream in = new ObjectInputStream(fis);	
				event_list = (ArrayList<Event>) in.readObject();
			}
			catch(Exception e)
			{
				return (new ArrayList<Event>());
			}
			
			return event_list;
		}
		
		//Write a new entry into the list
		
		public void write_Event(Event event)
		{
			String filename = "event.ser";
			event_list.add(event);
							
			try 
			{
				FileOutputStream fos = new FileOutputStream(filename);
				ObjectOutputStream out = new ObjectOutputStream(fos);
				out.writeObject(event_list);
				out.close();
			}
			catch(Exception e)
			{
				
			}
		}
		
		public void message_event()
		{
			Event event;
			
			ArrayList<Event> list = read_Event();
			
			for(int i = 0; i < list.size(); i++)
			{
				JOptionPane.showMessageDialog(null, "Saved numbers: \n\n" + list.get(i).getAdditionalNotes());
			}
			
		}
		@Override
		public String getShortName() {
			return "EventController";
		}

		@Override
		public void update(Subject o) {
			// TODO Auto-generated method stub
			
		}

}